<!--
 * @Description: 
 * @Autor: zzj
 * @Date: 2023-08-29 15:56:52
 * @LastEditors: zzj
 * @LastEditTime: 2023-09-10 14:09:37
 * @FilePath: \vite-project\src\views\login\index.vue
-->
<template>
  <div class="login_container">
    <el-row>
      <!-- xs表示，屏幕小于768的时候的占位 -->
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          ref="loginForms"
          :model="loginForm"
          :rules="rules"
          class="login_form"
        >
          <h1>Hello</h1>
          <h2>欢迎来到尚品汇！</h2>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              :prefix-icon="User"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <!-- show-password是否显示切换密码图标 -->
            <el-input
              type="password"
              v-model="loginForm.password"
              :prefix-icon="Lock"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              v-loading="loading"
              class="login_btn"
              type="primary"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import useUserStore from '@/store/modules/user'
import { useRouter } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/time'
let userStore = useUserStore()
let $router = useRouter()
// 收集账号和密码的数据
let loginForm = reactive({ username: 'admin', password: '111111' })
const validatorUserName = (rule: any, value: any, callback: any) => {
  // callback:如果符合条件放行，不符合注入错误提示信息
  if (value.length > 4) {
    callback()
  } else {
    callback(new Error('账号长度至少5位'))
  }
}
const validatorPassWord = (rule: any, value: any, callback: any) => {
  // callback:如果符合条件放行，不符合注入错误提示信息
  if (value.length > 5) {
    callback()
  } else {
    callback(new Error('密码长度至少6位'))
  }
}
// 表单校验
const rules = {
  username: [{ tigger: 'change', validator: validatorUserName }],
  password: [{ tigger: 'change', validator: validatorPassWord }],
}
// 定义变量控制按钮加载效果
let loading = ref(false)
let loginForms = ref()
// 登录
const login = async () => {
  await loginForms.value.validate()
  loading.value = true
  // 点击登录按钮以后干什么？
  // 通知仓库发登录请求  成功==》跳转首页展示数据  失败==》弹出登录失败信息
  try {
    // 也可以.then写法
    await userStore.userLogin(loginForm)
    $router.push('/')
    // 成功提示
    ElNotification({
      type: 'success',
      message: '欢迎回来！',
      title: `HI,${getTime()}好`,
    })
  } catch (error) {
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
  }
  loading.value = false
}
</script>

<style lang="scss" scoped>
.login_container {
  width: 100vw;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      color: #fff;
      font-size: 20px;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
